package com.instasale.dao;

import com.instasale.models.Seller;
import org.hibernate.SessionFactory;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class HibernateSellerDAO implements SellerDAO {

    private HibernateTemplate hibernateTemplate;

    public void setSessionFactory(SessionFactory sessionFactory) {
        this.hibernateTemplate = new HibernateTemplate(sessionFactory);
    }

    @Override
    public void addSeller(Seller seller) {
        hibernateTemplate.saveOrUpdate(seller);
    }

    @Override
    public void removeSeller(Integer id) {
        Seller Seller = hibernateTemplate.load(Seller.class, id);
        if (Seller != null) {
            hibernateTemplate.delete(Seller);
        }
    }

    @Override
    public Seller getById(Integer id) {
        return hibernateTemplate.get(Seller.class, id);
    }

    @Override
    public List<Seller> listSeller() {
        return hibernateTemplate.find("from " + Seller.class.getName());
    }
}
